/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ve.com.cantv.worktables.ejb.session;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import ve.com.cantv.worktables.ejb.entity.UUser;

/**
 *
 * @author Ghust
 */
@Stateless
public class UUserFacade implements UUserFacadeRemote {
    @PersistenceContext
    private EntityManager em;

    public void create(UUser uUser) {
        em.persist(uUser);
    }

    public void edit(UUser uUser) {
        em.merge(uUser);
    }

    public void remove(UUser uUser) {
        em.remove(em.merge(uUser));
    }

    public UUser find(Object id) {
        return em.find(UUser.class, id);
    }

    public List<UUser> findAll() {
        return em.createQuery("select object(o) from UUser as o").getResultList();
    }

    public UUser findByLogin(String login){
        Object obj = null;
        try{
        
        obj = em.createQuery("select distinct o from UUser o join fetch o.rolmoduleCollection join fetch o.moduleCollection where o.idusersap like :login ").
                setParameter("login", login).getSingleResult();
            
        }
        catch(NoResultException e){
            e.printStackTrace();
        }
        catch(Exception ex){
            ex.printStackTrace();
        }

        if(obj != null && obj instanceof UUser){
            UUser retval = (UUser)obj;
            return retval;
        }
        else{
            return null;
        }
    }

    



}
